package com.wanlian.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import org.jasypt.util.text.BasicTextEncryptor;

public class EncryptOperation {

	public static String eccryptByMd5(String info) throws NoSuchAlgorithmException {
		// 根据MD5算法生成MessageDigest对象
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		byte[] srcBytes = info.getBytes();
		// 使用srcBytes更新摘要
		md5.update(srcBytes);
		// 完成哈希计算，得到result
		byte[] resultBytes = md5.digest();
		String result = new String(resultBytes);
		return result;
	}

	public static String eccryptBySHA(String info) throws NoSuchAlgorithmException {
		MessageDigest md5 = MessageDigest.getInstance("SHA");
		byte[] srcBytes = info.getBytes();
		// 使用srcBytes更新摘要
		md5.update(srcBytes);
		// 完成哈希计算，得到result
		byte[] resultBytes = md5.digest();
		String result = new String(resultBytes);
		return result;
	}

	public static String pwdEncryption(String plainTextPwd, String key) {
		// PBEWithMD5AndDES
		BasicTextEncryptor encryptor = new BasicTextEncryptor();
		encryptor.setPassword(key);
		return encryptor.encrypt(plainTextPwd);
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入待加密内容: ");
		String source = sc.nextLine();
		System.out.println("请输入key: ");
		String key = sc.nextLine();
		String result = pwdEncryption(source, key);
		System.out.println("加密后: " + result);
		sc.close();
	}
}
